/*
 * @FileName: RewardService.java
 * @Author
 * @Description:
 * @Date 2016年1月13日 下午4:36:36
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Business;
import cn.hpclub.server.model.Reward;
import cn.hpclub.server.model.Website;

/**
 * Class description goes here
 *
 * @author
 */
public class RewardService{
    public static RewardService service = new RewardService();

    /**
     * 搜索条件封装成map
     * 
     * @return
     */
    public Page<Record> list(int num, int size, ConcurrentHashMap<String, String> map){

        String busName = (map != null && map.containsKey(Business.BUSINESS_NAME)) ? map.get(Business.BUSINESS_NAME)
                : "";
        String busCode = (map != null && map.containsKey(Business.BUSINESS_CODE)) ? map.get(Business.BUSINESS_CODE)
                : "";
        int orgId = (map != null && map.containsKey(Reward.ORG_ID)) ? Integer.parseInt(map.get(Reward.ORG_ID)) : -1;

        return Reward.dao.list(num, size, orgId, busName, busCode);
    }

    public List<String> getChannel(int id){
        List<Record> list = Reward.dao.getChannel(id);
        List<String> channel = new ArrayList<String>();

        StringBuffer temp = new StringBuffer();
        StringBuffer channel_id = new StringBuffer();
        for(int i = 0; i < list.size(); i++){
            temp.append("/" + list.get(i).getStr(Website.WEBSITE_ID));
            channel_id.append("/" + list.get(i).getInt(Reward.CHANNELID));
        }
        if(temp.length() >= 1){
            temp = new StringBuffer(temp.substring(1));
        }
        if(channel_id.length() >= 1){
            channel_id = new StringBuffer(channel_id.substring(1));
        }
        channel.add(temp.toString());
        channel.add(channel_id.toString());
        return channel;
    }

    /**
     * 可以根据渠道类型id和渠道类型名称进行筛选
     * @param check_enabled
     * @param searchBy
     * @return
     */
    public List<Record> getWebsite(boolean check_enabled,String searchBy){
        try{
        	String sql = "select * from " + Constant.Table.ORGANIZATION_CLASS + " where (WebsiteId like ? or class_name like ?)";
            if(check_enabled){
            	sql+=" and "+Reward.ENABLED+"='1'";
            }
            return Db.find(sql, "%"+searchBy+"%","%"+searchBy+"%");

        } catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
